from fastapi import APIRouter, Depends
from fastapi.responses import StreamingResponse
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
from pydantic import BaseModel
from sqlalchemy.orm import Session
from api.dep import get_current_active_user
from db_manage.conversation_manage import create_message, get_messages, delete_message
from database import get_db
from models import SysUser
from services.conversation_service import chat

router = APIRouter(prefix="/api", tags=["chat"])

security = HTTPBearer()

class ChatForm(BaseModel):
    conversation_id: int
    content: str
@router.post("/chat")
async def create_message_route(
    chat_form: ChatForm,
    current_user: SysUser = Depends(get_current_active_user),
    db: Session = Depends(get_db)
    ):
    return StreamingResponse(
        content=chat(db=db, conversation_id=chat_form.conversation_id, user_message=chat_form.content, user_id=current_user.id))

@router.get("/chat/{conversation_id}")
def read_messages_route(
    conversation_id: int,
    current_user: SysUser = Depends(get_current_active_user),
    db: Session = Depends(get_db)):
    return get_messages(db=db, conversation_id=conversation_id, user_id=current_user.id)

@router.delete("/chat/{message_id}")
def delete_message_route(
    message_id: int,
    current_user: SysUser = Depends(get_current_active_user),
    db: Session = Depends(get_db)):
    return delete_message(db=db, message_id=message_id, user_id=current_user.id)
